removed $wgParserCache, converted to a singleton
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
8 */
9 if( defined( 'MEDIAWIKI' ) ) {
10
11 # See design.txt
12
13 if($wgUseTeX) require_once( 'Math.php' );
14
15 /**
16 * @todo document
17 * @package MediaWiki
18 */
19 class OutputPage {
20 var $mHeaders, $mMetatags, $mKeywords;
21 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
22 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
23 var $mSubtitle, $mRedirect, $mStatusCode;
24 var $mLastModified, $mETag, $mCategoryLinks;
25 var $mScripts, $mLinkColours;
26
27 var $mSuppressQuickbar;
28 var $mOnloadHandler;
29 var $mDoNothing;
30 var $mContainsOldMagic, $mContainsNewMagic;
31 var $mIsArticleRelated;
32 var $mParserOptions;
33 var $mShowFeedLinks = false;
34 var $mEnableClientCache = true;
35 var $mArticleBodyOnly = false;
36
37 /**
38 * Constructor
39 * Initialise private variables
40 */
41 function OutputPage() {
42 $this->mHeaders = $this->mMetatags =
43 $this->mKeywords = $this->mLinktags = array();
44 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
45 $this->mRedirect = $this->mLastModified =
46 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
47 $this->mOnloadHandler = '';
48 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
49 $this->mSuppressQuickbar = $this->mPrintable = false;
50 $this->mLanguageLinks = array();
51 $this->mCategoryLinks = array() ;
52 $this->mDoNothing = false;
53 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
54 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
55 $this->mSquidMaxage = 0;
56 $this->mScripts = '';
57 $this->mETag = false;
58 $this->mRevisionId = null;
59 }
60
61 function addHeader( $name, $val ) { array_push( $this->mHeaders, $name.': '.$val ) ; }
62 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
63 function setStatusCode( $statusCode ) { $this->mStatusCode = $statusCode; }
64
65 # To add an http-equiv meta tag, precede the name with "http:"
66 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
67 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
68 function addScript( $script ) { $this->mScripts .= $script; }
69 function getScript() { return $this->mScripts; }
70
71 function setETag($tag) { $this->mETag = $tag; }
72 function setArticleBodyOnly($only) { $this->mArticleBodyOnly = $only; }
73 function getArticleBodyOnly($only) { return $this->mArticleBodyOnly; }
74
75 function addLink( $linkarr ) {
76 # $linkarr should be an associative array of attributes. We'll escape on output.
77 array_push( $this->mLinktags, $linkarr );
78 }
79
80 function addMetadataLink( $linkarr ) {
81 # note: buggy CC software only reads first "meta" link
82 static $haveMeta = false;
83 $linkarr['rel'] = ($haveMeta) ? 'alternate meta' : 'meta';
84 $this->addLink( $linkarr );
85 $haveMeta = true;
86 }
87
88 /**
89 * checkLastModified tells the client to use the client-cached page if
90 * possible. If sucessful, the OutputPage is disabled so that
91 * any future call to OutputPage->output() have no effect. The method
92 * returns true iff cache-ok headers was sent.
93 */
94 function checkLastModified ( $timestamp ) {
95 global $wgCachePages, $wgUser;
96 if ( !$timestamp || $timestamp == '19700101000000' ) {
97 wfDebug( "CACHE DISABLED, NO TIMESTAMP\n" );
98 return;
99 }
100 if( !$wgCachePages ) {
101 wfDebug( "CACHE DISABLED\n", false );
102 return;
103 }
104 if( $wgUser->getOption( 'nocache' ) ) {
105 wfDebug( "USER DISABLED CACHE\n", false );
106 return;
107 }
108
109 $timestamp=wfTimestamp(TS_MW,$timestamp);
110 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched ) );
111
112 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
113 # IE sends sizes after the date like this:
114 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
115 # this breaks strtotime().
116 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
117 $modsinceTime = strtotime( $modsince );
118 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
119 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
120 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
121 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) ) {
122 # Make sure you're in a place you can leave when you call us!
123 header( "HTTP/1.0 304 Not Modified" );
124 $this->mLastModified = $lastmod;
125 $this->sendCacheControl();
126 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
127 $this->disable();
128 @ob_end_clean(); // Don't output compressed blob
129 return true;
130 } else {
131 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
132 $this->mLastModified = $lastmod;
133 }
134 } else {
135 wfDebug( "client did not send If-Modified-Since header\n", false );
136 $this->mLastModified = $lastmod;
137 }
138 }
139
140 function getPageTitleActionText () {
141 global $action;
142 switch($action) {
143 case 'edit':
144 case 'delete':
145 case 'protect':
146 case 'unprotect':
147 case 'watch':
148 case 'unwatch':
149 // Display title is already customized
150 return '';
151 case 'history':
152 return wfMsg('history_short');
153 case 'submit':
154 // FIXME: bug 2735; not correct for special pages etc
155 return wfMsg('preview');
156 case 'info':
157 return wfMsg('info_short');
158 default:
159 return '';
160 }
161 }
162
163 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
164 function setHTMLTitle( $name ) {$this->mHTMLtitle = $name; }
165 function setPageTitle( $name ) {
166 global $action, $wgContLang;
167 $name = $wgContLang->convert($name, true);
168 $this->mPagetitle = $name;
169 if(!empty($action)) {
170 $taction = $this->getPageTitleActionText();
171 if( !empty( $taction ) ) {
172 $name .= ' - '.$taction;
173 }
174 }
175
176 $this->setHTMLTitle( wfMsg( 'pagetitle', $name ) );
177 }
178 function getHTMLTitle() { return $this->mHTMLtitle; }
179 function getPageTitle() { return $this->mPagetitle; }
180 function setSubtitle( $str ) { $this->mSubtitle = /*$this->parse(*/$str/*)*/; } // @bug 2514
181 function getSubtitle() { return $this->mSubtitle; }
182 function isArticle() { return $this->mIsarticle; }
183 function setPrintable() { $this->mPrintable = true; }
184 function isPrintable() { return $this->mPrintable; }
185 function setSyndicated( $show = true ) { $this->mShowFeedLinks = $show; }
186 function isSyndicated() { return $this->mShowFeedLinks; }
187 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
188 function getOnloadHandler() { return $this->mOnloadHandler; }
189 function disable() { $this->mDoNothing = true; }
190
191 function setArticleRelated( $v ) {
192 $this->mIsArticleRelated = $v;
193 if ( !$v ) {
194 $this->mIsarticle = false;
195 }
196 }
197 function setArticleFlag( $v ) {
198 $this->mIsarticle = $v;
199 if ( $v ) {
200 $this->mIsArticleRelated = $v;
201 }
202 }
203
204 function isArticleRelated() { return $this->mIsArticleRelated; }
205
206 function getLanguageLinks() { return $this->mLanguageLinks; }
207 function addLanguageLinks($newLinkArray) {
208 $this->mLanguageLinks += $newLinkArray;
209 }
210 function setLanguageLinks($newLinkArray) {
211 $this->mLanguageLinks = $newLinkArray;
212 }
213
214 function getCategoryLinks() {
215 return $this->mCategoryLinks;
216 }
217
218 /**
219 * Add an array of categories, with names in the keys
220 */
221 function addCategoryLinks($categories) {
222 global $wgUser, $wgContLang;
223
224 # Add the links to the link cache in a batch
225 $arr = array( NS_CATEGORY => $categories );
226 $lb = new LinkBatch;
227 $lb->setArray( $arr );
228 $lb->execute();
229
230 $sk =& $wgUser->getSkin();
231 foreach ( $categories as $category => $arbitrary ) {
232 $title = Title::makeTitleSafe( NS_CATEGORY, $category );
233 $text = $wgContLang->convertHtml( $title->getText() );
234 $this->mCategoryLinks[] = $sk->makeLinkObj( $title, $text );
235 }
236 }
237
238 function setCategoryLinks($categories) {
239 $this->mCategoryLinks = array();
240 $this->addCategoryLinks($categories);
241 }
242
243 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
244 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
245
246 function addHTML( $text ) { $this->mBodytext .= $text; }
247 function clearHTML() { $this->mBodytext = ''; }
248 function getHTML() { return $this->mBodytext; }
249 function debug( $text ) { $this->mDebugtext .= $text; }
250
251 /* @deprecated */
252 function setParserOptions( $options ) {
253 return $this->ParserOptions( $options );
254 }
255
256 function ParserOptions( $options = null ) {
257 return wfSetVar( $this->mParserOptions, $options );
258 }
259
260 /**
261 * Set the revision ID which will be seen by the wiki text parser
262 * for things such as embedded {{REVISIONID}} variable use.
263 * @param mixed $revid an integer, or NULL
264 * @return mixed previous value
265 */
266 function setRevisionId( $revid ) {
267 $val = is_null( $revid ) ? null : intval( $revid );
268 return wfSetVar( $this->mRevisionId, $val );
269 }
270
271 /**
272 * Convert wikitext to HTML and add it to the buffer
273 * Default assumes that the current page title will
274 * be used.
275 */
276 function addWikiText( $text, $linestart = true ) {
277 global $wgTitle;
278 $this->addWikiTextTitle($text, $wgTitle, $linestart);
279 }
280
281 function addWikiTextWithTitle($text, &$title, $linestart = true) {
282 $this->addWikiTextTitle($text, $title, $linestart);
283 }
284
285 function addWikiTextTitle($text, &$title, $linestart) {
286 global $wgParser;
287 $parserOutput = $wgParser->parse( $text, $title, $this->mParserOptions,
288 $linestart, true, $this->mRevisionId );
289 $this->addParserOutput( $parserOutput );
290 }
291
292 function addParserOutputNoText( &$parserOutput ) {
293 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
294 $this->addCategoryLinks( $parserOutput->getCategories() );
295 $this->addKeywords( $parserOutput );
296 if ( $parserOutput->getCacheTime() == -1 ) {
297 $this->enableClientCache( false );
298 }
299 }
300
301 function addParserOutput( &$parserOutput ) {
302 $this->addParserOutputNoText( $parserOutput );
303 $this->addHTML( $parserOutput->getText() );
304 }
305
306 /**
307 * Add wikitext to the buffer, assuming that this is the primary text for a page view
308 * Saves the text into the parser cache if possible
309 */
310 function addPrimaryWikiText( $text, $article, $cache = true ) {
311 global $wgParser, $wgUser;
312
313 $parserOutput = $wgParser->parse( $text, $article->mTitle,
314 $this->mParserOptions, true, true, $this->mRevisionId );
315
316 if ( $article && $parserOutput->getCacheTime() != -1 ) {
317 $parserCache =& ParserCache::singleton();
318 $parserCache->save( $parserOutput, $article, $wgUser );
319 }
320
321 $this->addParserOutput( $parserOutput );
322 }
323
324 /**
325 * Add the output of a QuickTemplate to the output buffer
326 * @param QuickTemplate $template
327 */
328 function addTemplate( &$template ) {
329 ob_start();
330 $template->execute();
331 $this->addHTML( ob_get_contents() );
332 ob_end_clean();
333 }
334
335 /**
336 * Parse wikitext and return the HTML. This is for special pages that add the text later
337 */
338 function parse( $text, $linestart = true ) {
339 global $wgParser, $wgTitle;
340 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions,
341 $linestart, true, $this->mRevisionId );
342 return $parserOutput->getText();
343 }
344
345 /**
346 * @param $article
347 * @param $user
348 *
349 * @return bool
350 */
351 function tryParserCache( $article, $user ) {
352 $parserCache =& ParserCache::singleton();
353 $parserOutput = $parserCache->get( $article, $user );
354 if ( $parserOutput !== false ) {
355 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
356 $this->addCategoryLinks( $parserOutput->getCategories() );
357 $this->addKeywords( $parserOutput );
358 $this->addHTML( $parserOutput->getText() );
359 $t = $parserOutput->getTitleText();
360 if( !empty( $t ) ) {
361 $this->setPageTitle( $t );
362 }
363 return true;
364 } else {
365 return false;
366 }
367 }
368
369 /**
370 * Set the maximum cache time on the Squid in seconds
371 * @param $maxage
372 */
373 function setSquidMaxage( $maxage ) {
374 $this->mSquidMaxage = $maxage;
375 }
376
377 /**
378 * Use enableClientCache(false) to force it to send nocache headers
379 * @param $state
380 */
381 function enableClientCache( $state ) {
382 return wfSetVar( $this->mEnableClientCache, $state );
383 }
384
385 function uncacheableBecauseRequestvars() {
386 global $wgRequest;
387 return $wgRequest->getText('useskin', false) === false
388 && $wgRequest->getText('uselang', false) === false;
389 }
390
391 function sendCacheControl() {
392 global $wgUseSquid, $wgUseESI;
393
394 if ($this->mETag)
395 header("ETag: $this->mETag");
396
397 # don't serve compressed data to clients who can't handle it
398 # maintain different caches for logged-in users and non-logged in ones
399 header( 'Vary: Accept-Encoding, Cookie' );
400 if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) {
401 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
402 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
403 {
404 if ( $wgUseESI ) {
405 # We'll purge the proxy cache explicitly, but require end user agents
406 # to revalidate against the proxy on each visit.
407 # Surrogate-Control controls our Squid, Cache-Control downstream caches
408 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
409 # start with a shorter timeout for initial testing
410 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
411 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
412 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
413 } else {
414 # We'll purge the proxy cache for anons explicitly, but require end user agents
415 # to revalidate against the proxy on each visit.
416 # IMPORTANT! The Squid needs to replace the Cache-Control header with
417 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
418 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
419 # start with a shorter timeout for initial testing
420 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
421 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
422 }
423 } else {
424 # We do want clients to cache if they can, but they *must* check for updates
425 # on revisiting the page.
426 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
427 header( "Expires: -1" );
428 header( "Cache-Control: private, must-revalidate, max-age=0" );
429 }
430 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
431 } else {
432 wfDebug( "** no caching **\n", false );
433
434 # In general, the absence of a last modified header should be enough to prevent
435 # the client from using its cache. We send a few other things just to make sure.
436 header( 'Expires: -1' );
437 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
438 header( 'Pragma: no-cache' );
439 }
440 }
441
442 /**
443 * Finally, all the text has been munged and accumulated into
444 * the object, let's actually output it:
445 */
446 function output() {
447 global $wgUser, $wgOutputEncoding;
448 global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType, $wgProfiler;
449
450 if( $this->mDoNothing ){
451 return;
452 }
453 $fname = 'OutputPage::output';
454 wfProfileIn( $fname );
455 $sk = $wgUser->getSkin();
456
457 if ( '' != $this->mRedirect ) {
458 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
459 # Standards require redirect URLs to be absolute
460 global $wgServer;
461 $this->mRedirect = $wgServer . $this->mRedirect;
462 }
463 if( $this->mRedirectCode == '301') {
464 if( !$wgDebugRedirects ) {
465 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
466 }
467 $this->mLastModified = wfTimestamp( TS_RFC2822 );
468 }
469
470 $this->sendCacheControl();
471
472 if( $wgDebugRedirects ) {
473 $url = htmlspecialchars( $this->mRedirect );
474 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
475 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
476 print "</body>\n</html>\n";
477 } else {
478 header( 'Location: '.$this->mRedirect );
479 }
480 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
481 wfProfileOut( $fname );
482 return;
483 }
484 elseif ( $this->mStatusCode )
485 {
486 $statusMessage = array(
487 100 => 'Continue',
488 101 => 'Switching Protocols',
489 102 => 'Processing',
490 200 => 'OK',
491 201 => 'Created',
492 202 => 'Accepted',
493 203 => 'Non-Authoritative Information',
494 204 => 'No Content',
495 205 => 'Reset Content',
496 206 => 'Partial Content',
497 207 => 'Multi-Status',
498 300 => 'Multiple Choices',
499 301 => 'Moved Permanently',
500 302 => 'Found',
501 303 => 'See Other',
502 304 => 'Not Modified',
503 305 => 'Use Proxy',
504 307 => 'Temporary Redirect',
505 400 => 'Bad Request',
506 401 => 'Unauthorized',
507 402 => 'Payment Required',
508 403 => 'Forbidden',
509 404 => 'Not Found',
510 405 => 'Method Not Allowed',
511 406 => 'Not Acceptable',
512 407 => 'Proxy Authentication Required',
513 408 => 'Request Timeout',
514 409 => 'Conflict',
515 410 => 'Gone',
516 411 => 'Length Required',
517 412 => 'Precondition Failed',
518 413 => 'Request Entity Too Large',
519 414 => 'Request-URI Too Large',
520 415 => 'Unsupported Media Type',
521 416 => 'Request Range Not Satisfiable',
522 417 => 'Expectation Failed',
523 422 => 'Unprocessable Entity',
524 423 => 'Locked',
525 424 => 'Failed Dependency',
526 500 => 'Internal Server Error',
527 501 => 'Not Implemented',
528 502 => 'Bad Gateway',
529 503 => 'Service Unavailable',
530 504 => 'Gateway Timeout',
531 505 => 'HTTP Version Not Supported',
532 507 => 'Insufficient Storage'
533 );
534
535 if ( $statusMessage[$this->mStatusCode] )
536 header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $statusMessage[$this->mStatusCode] );
537 }
538
539 # Buffer output; final headers may depend on later processing
540 ob_start();
541
542 # Disable temporary placeholders, so that the skin produces HTML
543 $sk->postParseLinkColour( false );
544
545 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
546 header( 'Content-language: '.$wgContLanguageCode );
547
548 if ($this->mArticleBodyOnly) {
549 $this->out($this->mBodytext);
550 } else {
551 wfProfileIn( 'Output-skin' );
552 $sk->outputPage( $this );
553 wfProfileOut( 'Output-skin' );
554 }
555
556 $this->sendCacheControl();
557 ob_end_flush();
558 wfProfileOut( $fname );
559 }
560
561 function out( $ins ) {
562 global $wgInputEncoding, $wgOutputEncoding, $wgContLang;
563 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
564 $outs = $ins;
565 } else {
566 $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
567 if ( false === $outs ) { $outs = $ins; }
568 }
569 print $outs;
570 }
571
572 function setEncodings() {
573 global $wgInputEncoding, $wgOutputEncoding;
574 global $wgUser, $wgContLang;
575
576 $wgInputEncoding = strtolower( $wgInputEncoding );
577
578 if( $wgUser->getOption( 'altencoding' ) ) {
579 $wgContLang->setAltEncoding();
580 return;
581 }
582
583 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
584 $wgOutputEncoding = strtolower( $wgOutputEncoding );
585 return;
586 }
587 $wgOutputEncoding = $wgInputEncoding;
588 }
589
590 /**
591 * Returns a HTML comment with the elapsed time since request.
592 * This method has no side effects.
593 * Use wfReportTime() instead.
594 * @return string
595 * @deprecated
596 */
597 function reportTime() {
598 $time = wfReportTime();
599 return $time;
600 }
601
602 /**
603 * Note: these arguments are keys into wfMsg(), not text!
604 */
605 function errorpage( $title, $msg ) {
606 global $wgTitle;
607
608 $this->mDebugtext .= 'Original title: ' .
609 $wgTitle->getPrefixedText() . "\n";
610 $this->setPageTitle( wfMsg( $title ) );
611 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
612 $this->setRobotpolicy( 'noindex,nofollow' );
613 $this->setArticleRelated( false );
614 $this->enableClientCache( false );
615 $this->mRedirect = '';
616
617 $this->mBodytext = '';
618 $this->addWikiText( wfMsg( $msg ) );
619 $this->returnToMain( false );
620
621 $this->output();
622 wfErrorExit();
623 }
624
625 /**
626 * Display an error page indicating that a given version of MediaWiki is
627 * required to use it
628 *
629 * @param mixed $version The version of MediaWiki needed to use the page
630 */
631 function versionRequired( $version ) {
632 global $wgUser;
633
634 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
635 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
636 $this->setRobotpolicy( 'noindex,nofollow' );
637 $this->setArticleRelated( false );
638 $this->mBodytext = '';
639
640 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
641 $this->returnToMain();
642 }
643
644 /**
645 * Display an error page noting that a given permission bit is required.
646 * This should generally replace the sysopRequired, developerRequired etc.
647 * @param string $permission key required
648 */
649 function permissionRequired( $permission ) {
650 global $wgUser;
651
652 $this->setPageTitle( wfMsg( 'badaccess' ) );
653 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
654 $this->setRobotpolicy( 'noindex,nofollow' );
655 $this->setArticleRelated( false );
656 $this->mBodytext = '';
657
658 $sk = $wgUser->getSkin();
659 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ) );
660 $this->addHTML( wfMsgHtml( 'badaccesstext', $ap, $permission ) );
661 $this->returnToMain();
662 }
663
664 /**
665 * @deprecated
666 */
667 function sysopRequired() {
668 global $wgUser;
669
670 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
671 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
672 $this->setRobotpolicy( 'noindex,nofollow' );
673 $this->setArticleRelated( false );
674 $this->mBodytext = '';
675
676 $sk = $wgUser->getSkin();
677 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
678 $this->addHTML( wfMsgHtml( 'sysoptext', $ap ) );
679 $this->returnToMain();
680 }
681
682 /**
683 * @deprecated
684 */
685 function developerRequired() {
686 global $wgUser;
687
688 $this->setPageTitle( wfMsg( 'developertitle' ) );
689 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
690 $this->setRobotpolicy( 'noindex,nofollow' );
691 $this->setArticleRelated( false );
692 $this->mBodytext = '';
693
694 $sk = $wgUser->getSkin();
695 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
696 $this->addHTML( wfMsgHtml( 'developertext', $ap ) );
697 $this->returnToMain();
698 }
699
700 function loginToUse() {
701 global $wgUser, $wgTitle, $wgContLang;
702
703 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
704 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
705 $this->setRobotpolicy( 'noindex,nofollow' );
706 $this->setArticleFlag( false );
707 $this->mBodytext = '';
708 $loginpage = Title::makeTitle(NS_SPECIAL, 'Userlogin');
709 $sk = $wgUser->getSkin();
710 $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'),
711 'returnto=' . htmlspecialchars($wgTitle->getPrefixedDBkey()));
712 $this->addHTML( wfMsgHtml( 'loginreqpagetext', $loginlink ) );
713
714 # We put a comment in the .html file so a Sysop can diagnose the page the
715 # user can't see.
716 $this->addHTML( "\n<!--" .
717 $wgContLang->getNsText( $wgTitle->getNamespace() ) .
718 ':' .
719 $wgTitle->getDBkey() . '-->' );
720 $this->returnToMain(); # Flip back to the main page after 10 seconds.
721 }
722
723 function databaseError( $fname, $sql, $error, $errno ) {
724 global $wgUser, $wgCommandLineMode, $wgShowSQLErrors;
725
726 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
727 $this->setRobotpolicy( 'noindex,nofollow' );
728 $this->setArticleRelated( false );
729 $this->enableClientCache( false );
730 $this->mRedirect = '';
731
732 if( !$wgShowSQLErrors ) {
733 $sql = wfMsg( 'sqlhidden' );
734 }
735
736 if ( $wgCommandLineMode ) {
737 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
738 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
739 } else {
740 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
741 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
742 }
743
744 if ( $wgCommandLineMode || !is_object( $wgUser )) {
745 print $msg."\n";
746 wfErrorExit();
747 }
748 $this->mBodytext = $msg;
749 $this->output();
750 wfErrorExit();
751 }
752
753 function readOnlyPage( $source = null, $protected = false ) {
754 global $wgUser, $wgReadOnlyFile, $wgReadOnly;
755
756 $this->setRobotpolicy( 'noindex,nofollow' );
757 $this->setArticleRelated( false );
758
759 if( $protected ) {
760 $this->setPageTitle( wfMsg( 'viewsource' ) );
761 $this->addWikiText( wfMsg( 'protectedtext' ) );
762 } else {
763 $this->setPageTitle( wfMsg( 'readonly' ) );
764 if ( $wgReadOnly ) {
765 $reason = $wgReadOnly;
766 } else {
767 $reason = file_get_contents( $wgReadOnlyFile );
768 }
769 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
770 }
771
772 if( is_string( $source ) ) {
773 if( strcmp( $source, '' ) == 0 ) {
774 global $wgTitle ;
775 if ( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
776 $source = wfMsgWeirdKey ( $wgTitle->getText() ) ;
777 } else {
778 $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
779 }
780 }
781 $rows = $wgUser->getOption( 'rows' );
782 $cols = $wgUser->getOption( 'cols' );
783 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
784 htmlspecialchars( $source ) . "\n</textarea>";
785 $this->addHTML( $text );
786 }
787
788 $this->returnToMain( false );
789 }
790
791 function fatalError( $message ) {
792 $this->setPageTitle( wfMsg( "internalerror" ) );
793 $this->setRobotpolicy( "noindex,nofollow" );
794 $this->setArticleRelated( false );
795 $this->enableClientCache( false );
796 $this->mRedirect = '';
797
798 $this->mBodytext = $message;
799 $this->output();
800 wfErrorExit();
801 }
802
803 function unexpectedValueError( $name, $val ) {
804 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
805 }
806
807 function fileCopyError( $old, $new ) {
808 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
809 }
810
811 function fileRenameError( $old, $new ) {
812 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
813 }
814
815 function fileDeleteError( $name ) {
816 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
817 }
818
819 function fileNotFoundError( $name ) {
820 $this->fatalError( wfMsg( 'filenotfound', $name ) );
821 }
822
823 /**
824 * return from error messages or notes
825 * @param $auto automatically redirect the user after 10 seconds
826 * @param $returnto page title to return to. Default is Main Page.
827 */
828 function returnToMain( $auto = true, $returnto = NULL ) {
829 global $wgUser, $wgOut, $wgRequest;
830
831 if ( $returnto == NULL ) {
832 $returnto = $wgRequest->getText( 'returnto' );
833 }
834 $returnto = htmlspecialchars( $returnto );
835
836 $sk = $wgUser->getSkin();
837 if ( '' == $returnto ) {
838 $returnto = wfMsgForContent( 'mainpage' );
839 }
840 $link = $sk->makeKnownLink( $returnto, '' );
841
842 $r = wfMsg( 'returnto', $link );
843 if ( $auto ) {
844 $titleObj = Title::newFromText( $returnto );
845 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
846 }
847 $wgOut->addHTML( "\n<p>$r</p>\n" );
848 }
849
850 /**
851 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
852 * and uses the first 10 of them for META keywords
853 */
854 function addKeywords( &$parserOutput ) {
855 global $wgTitle;
856 $this->addKeyword( $wgTitle->getPrefixedText() );
857 $count = 1;
858 $links2d =& $parserOutput->getLinks();
859 foreach ( $links2d as $ns => $dbkeys ) {
860 foreach( $dbkeys as $dbkey => $id ) {
861 $this->addKeyword( $dbkey );
862 if ( ++$count > 10 ) {
863 break 2;
864 }
865 }
866 }
867 }
868
869 /**
870 * @private
871 * @return string
872 */
873 function headElement() {
874 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
875 global $wgUser, $wgContLang, $wgRequest, $wgUseTrackbacks, $wgTitle;
876
877 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
878 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
879 } else {
880 $ret = '';
881 }
882
883 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
884
885 if ( '' == $this->getHTMLTitle() ) {
886 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ));
887 }
888
889 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
890 $ret .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
891 $ret .= "<head>\n<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
892 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
893
894 $ret .= $this->getHeadLinks();
895 global $wgStylePath;
896 if( $this->isPrintable() ) {
897 $media = '';
898 } else {
899 $media = "media='print'";
900 }
901 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
902 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
903
904 $sk = $wgUser->getSkin();
905 $ret .= $sk->getHeadScripts();
906 $ret .= $this->mScripts;
907 $ret .= $sk->getUserStyles();
908
909 if ($wgUseTrackbacks && $this->isArticleRelated())
910 $ret .= $wgTitle->trackbackRDF();
911
912 $ret .= "</head>\n";
913 return $ret;
914 }
915
916 function getHeadLinks() {
917 global $wgRequest, $wgStylePath;
918 $ret = '';
919 foreach ( $this->mMetatags as $tag ) {
920 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
921 $a = 'http-equiv';
922 $tag[0] = substr( $tag[0], 5 );
923 } else {
924 $a = 'name';
925 }
926 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
927 }
928
929 $p = $this->mRobotpolicy;
930 if( $p !== '' && $p != 'index,follow' ) {
931 // http://www.robotstxt.org/wc/meta-user.html
932 // Only show if it's different from the default robots policy
933 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
934 }
935
936 if ( count( $this->mKeywords ) > 0 ) {
937 $strip = array(
938 "/<.*?>/" => '',
939 "/_/" => ' '
940 );
941 $ret .= "<meta name=\"keywords\" content=\"" .
942 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
943 }
944 foreach ( $this->mLinktags as $tag ) {
945 $ret .= '<link';
946 foreach( $tag as $attr => $val ) {
947 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
948 }
949 $ret .= " />\n";
950 }
951 if( $this->isSyndicated() ) {
952 # FIXME: centralize the mime-type and name information in Feed.php
953 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
954 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
955 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
956 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 0.3' href='$link' />\n";
957 }
958
959 return $ret;
960 }
961
962 /**
963 * Turn off regular page output and return an error reponse
964 * for when rate limiting has triggered.
965 * @todo i18n
966 * @access public
967 */
968 function rateLimited() {
969 global $wgOut;
970 $wgOut->disable();
971 wfHttpError( 500, 'Internal Server Error',
972 'Sorry, the server has encountered an internal error. ' .
973 'Please wait a moment and hit "refresh" to submit the request again.' );
974 }
975
976 }
977
978 } // MediaWiki
979
980 ?>